package com.example.clublesson01.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.CacheMemoryStaticUtils.put
import com.bw.kf.lib_base.base.BaseViewModel
import com.bw.kf.lib_base.entity.LoginEntity
import com.bw.kf.lib_base.entity.SendCodeEntity
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

class LoginRepo: BaseViewModel() {

    //账号密码登录
    val success= MutableLiveData<LoginEntity>()
    val fall=MutableLiveData<String>()
    fun Login(phone:String,password:String){
        viewModelScope.launch {
            try {
                val map = HashMap<String, Any>().apply {
                    put("grantType", "password")
                    put("newPassword",password)
                    put("operatorType",2)
                    put("password",password)
                    put("smsCode",0)
                    put("userName",phone)
                }
                success.value= apiService.Login(mapToJson(map))
            }catch(e:Exception) {
                fall.value=e.message
            }
        }
    }
    //获取验证码
    val successCode= MutableLiveData<SendCodeEntity>()
    val fallCode=MutableLiveData<String>()
    fun sendLoginCode(phone:String){
        viewModelScope.launch {
            try {
                successCode.value= apiService.sendLoginCode(operatorType = "2", phone = phone)
            }catch(e:Exception) {
                fallCode.value=e.message
            }
        }
    }
    //账号验证码登录
    val successLoginCode= MutableLiveData<LoginEntity>()
    val fallLoginCode=MutableLiveData<String>()
    fun LoginCode(phone:String,code:Int){
        viewModelScope.launch {
            try {
                val map = HashMap<String,Any>().apply {
                    put("grantType", "sms")
                    put("newPassword","")
                    put("operatorType",2)
                    put("password","")
                    put("smsCode",code)
                    put("userName",phone)
                }
                successLoginCode.value= apiService.LoginCode(mapToJson(map))
            }catch(e:Exception) {
                fallLoginCode.value=e.message
            }
        }
    }

    //忘记密码
    val successWCode=MutableLiveData<SendCodeEntity>()
    val failWCode=MutableLiveData<String>()

    fun WCode(phone:String){
        viewModelScope.launch {
            try {
                successWCode.value=apiService.Wang_Send(operatorType = "2", phone = phone)
            }catch (e:Exception){
                failWCode.value=e.message
            }
        }
    }


    //校验
    val successJYCode=MutableLiveData<SendCodeEntity>()
    val failJYCode=MutableLiveData<String>()
    fun JYCode(smsCode:Int,phone:String){
        viewModelScope.launch {
            try {
                val map=HashMap<String,Any>().apply {
                    put("grantType", "sms")
                    put("newPassword","")
                    put("operatorType",2)
                    put("password","")
                    put("smsCode",smsCode)
                    put("userName",phone)
                }
                successJYCode.value=apiService.resetPwd(mapToJson(map))
            }catch (e:Exception){
                failJYCode.value=e.message
            }
        }
    }

    //绑定
    val successBDCode=MutableLiveData<SendCodeEntity>()
    val failBDCode=MutableLiveData<String>()
    fun BDCode(smsCode:Int,phone:String,newPassword:String){
        viewModelScope.launch {
            try {
                val map=HashMap<String,Any>().apply {
                    put("grantType", "sms")
                    put("newPassword",newPassword)
                    put("operatorType",2)
                    put("password",newPassword)
                    put("smsCode",smsCode)
                    put("userName",phone)
                }
                successBDCode.value=apiService.resetNewPwd(mapToJson(map))
            }catch (e:Exception){
                failBDCode.value=e.message
            }
        }
    }

}